<template>
  <div class="login-container">
    <Header title="登录" show-back />

    <div class="login-content">
      <div class="logo-section">
        <div class="logo">
          <van-icon name="cloud-o" size="60" color="#1989fa" />
        </div>
        <h1 class="app-title">YY云盘</h1>
        <p class="app-desc">安全便捷的云存储服务</p>
      </div>

      <van-form @submit="handleLogin" class="login-form">
        <van-cell-group inset>
          <van-field
            v-model="loginForm.email"
            name="email"
            label="邮箱"
            placeholder="请输入邮箱"
            left-icon="envelop-o"
            :rules="emailRules"
            autocomplete="email"
          />
          <van-field
            v-model="loginForm.password"
            type="password"
            name="password"
            label="密码"
            placeholder="请输入密码"
            left-icon="lock"
            :rules="passwordRules"
            autocomplete="current-password"
          />
        </van-cell-group>

        <div class="login-actions">
          <van-button
            round
            block
            type="primary"
            native-type="submit"
            :loading="loading"
            loading-text="登录中..."
            class="login-button"
          >
            登录
          </van-button>
        </div>
      </van-form>

      <div class="bottom-links">
        <router-link to="/register" class="register-link">
          还没有账号？立即注册
        </router-link>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { showToast } from 'vant'
import Header from '@/components/Header.vue'
import { useUserStore } from '@/stores/user'
import type { LoginParams } from '@/api/user'

const router = useRouter()
const userStore = useUserStore()

const loading = ref(false)

const loginForm = reactive<LoginParams>({
  email: '',
  password: ''
})

const emailRules = [
  { required: true, message: '请输入邮箱' },
  { pattern: /^[^\s@]+@[^\s@]+\.[^\s@]+$/, message: '请输入正确的邮箱格式' }
]

const passwordRules = [
  { required: true, message: '请输入密码' },
  { min: 6, message: '密码长度不能少于6位' }
]

const handleLogin = async () => {
  try {
    loading.value = true

    await userStore.userLogin(loginForm)

    showToast({
      type: 'success',
      message: '登录成功',
      onClose: () => {
        router.replace('/')
      }
    })
  } catch (error) {
    console.error('登录失败:', error)

    let message = '登录失败'
    if (error instanceof Error) {
      if (error.message.includes('401')) {
        message = '邮箱或密码错误'
      } else if (error.message.includes('网络')) {
        message = '网络连接失败，请检查网络'
      } else {
        message = error.message
      }
    }

    showToast({
      type: 'fail',
      message
    })
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  flex-direction: column;
}

.login-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 20px;
  padding-top: 60px;
}

.logo-section {
  text-align: center;
  margin-bottom: 40px;
}

.logo {
  margin-bottom: 16px;
}

.app-title {
  font-size: 28px;
  font-weight: bold;
  color: white;
  margin: 0 0 8px 0;
}

.app-desc {
  font-size: 14px;
  color: rgba(255, 255, 255, 0.8);
  margin: 0;
}

.login-form {
  flex: 1;
}

.login-actions {
  padding: 24px 16px 16px;
}

.login-button {
  height: 44px;
  font-size: 16px;
  font-weight: 500;
}

.bottom-links {
  text-align: center;
  padding: 20px 0;
  margin-top: auto;
}

.register-link {
  color: rgba(255, 255, 255, 0.9);
  text-decoration: none;
  font-size: 14px;
}

.register-link:hover {
  color: white;
  text-decoration: underline;
}

:deep(.van-cell-group--inset) {
  margin: 0;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

:deep(.van-field__left-icon) {
  color: #1989fa;
}

:deep(.van-field__label) {
  color: #333;
  font-weight: 500;
}
</style>